package com.maxmlr.concurrent.cyclic;

import java.util.Random;

/**
 * @ClassName Consumer
 * @Description TODO
 * @Author maxiao
 * @Date 2020/3/5 上午11:23
 * @Version V1.0
 */
public class Consumer implements Runnable {

    private Message msg;

    public Consumer(Message msg) {
        this.msg = msg;
    }

    public String getKey() {
        return msg.key;
    }

    Random random = new Random();

    public Consumer(String key, String code) {
        msg = new Message();
        msg.key = key;
        msg.code = code;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"---"+msg.key + "开始消费");
        try {
            Thread.sleep(random.nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(msg.key + ":消费成功");

        Producer producer = TestCyclic.map.get(getKey());
        producer.setCode(msg.code);
        //这里表示闭锁，此线程处时完成，切换到发出事件线程。
        producer.countDownLatch.countDown();

    }
}
